﻿namespace ClientWF
{
    using System.ComponentModel;
    using System.Reflection;
    using System;
    using System.Windows.Forms;
    using System.Text;
    using System.Drawing;

    public class TicTacToe:IGame
    {
        string Nickname;
        string OpNickName;
        LoginRoom formToDisplayOn = null;
        public TicTacToe(LoginRoom form,string usernickname) 
        {
            formToDisplayOn = form;
                Nickname=usernickname;
        }        
        public override void startGame(string opponentNickName)
        {
            OpNickName = opponentNickName;
            formToDisplayOn.Height = 626;          
        }
        public override void updateGameStatus(string status)
        {
            //GamePanel.Dispose();
            string[] tempStr = status.Split('^');
            if (tempStr.Length > 2)
            {
                if (tempStr[2] == "Y")
                {
                    startGame(OpNickName);
                    formToDisplayOn.MoveLb.Text = Nickname + "`s Move";
                    redRawFiled(tempStr[0], true);
                }
                else
                {
                    formToDisplayOn.MoveLb.Text = OpNickName + "`s Move";
                    redRawFiled(tempStr[0], false);
                }
            }
            else
            {
                if (tempStr[1] == OpNickName)
                {
                    formToDisplayOn.MoveLb.Text = tempStr[1] + "`s Move";
                    redRawFiled(tempStr[0], false);
                }
                else
                {
                    formToDisplayOn.MoveLb.Text = tempStr[1] + "`s Move";
                    redRawFiled(tempStr[0], true);

                }
            }
        }
        public override void EndGame(string Result)
        {
            MessageBox.Show(Result);
            formToDisplayOn.LoggedView();
            formToDisplayOn.Height = 313;
            buttonsClear();
         
        }
        private void RemoveClickEvent(Button b)
        {
            FieldInfo f1 = typeof(Control).GetField("EventClick",
                BindingFlags.Static | BindingFlags.NonPublic);
            object obj = f1.GetValue(b);
            PropertyInfo pi = b.GetType().GetProperty("Events",
                BindingFlags.NonPublic | BindingFlags.Instance);
            EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
            list.RemoveHandler(obj, list[obj]);
        }
        void buttonsClear()
        {
            foreach (Button Btn in formToDisplayOn.ZeroCrossPanel.Controls)
            {
                RemoveClickEvent(Btn);
                Btn.Image = null;
            }
        }

        private void redRawFiled(string condition, bool move)
        {
            buttonsClear();
            string[] tempCon = condition.Split(',');
            formToDisplayOn.ZeroCrossPanel.BorderStyle = BorderStyle.FixedSingle;
            foreach (Button Btn in formToDisplayOn.ZeroCrossPanel.Controls)
            {
                for (int i = 0; i < tempCon.Length; i++)
                {
                    if (Btn.Name[3] == i.ToString()[0])
                    {
                        Btn.Tag = tempCon[i];
                    }
                }
                switch (Btn.Tag.ToString())
                {
                    case "0":
                        if (move)
                        {
                            Btn.Click += new System.EventHandler(moveAct);
                        };
                        break;
                    case "1":
                        Btn.Image = Properties.Resources.cross;
                        break;
                    default:
                        Btn.Image = Properties.Resources.zero;
                        break;
                }
            }

        }
        private void moveAct(object sender, EventArgs e)
        {
            int i = Int32.Parse(((System.Windows.Forms.Control)(sender)).Name[3].ToString());
            buttonsClear();
            formToDisplayOn._presenter.send("Move^" + Nickname + '^' + i.ToString());
        }

       
    }
}
